package shuashuami.hb.com.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * Created by Administrator on 2017/6/20.
 */

public class BohuiDialog extends Dialog {

    private Activity activity;
    private String id;

    public BohuiDialog(Activity context, String id) {
        super(context, R.style.Dialog);
        this.activity = context;
        this.id = id;
        setCustomDialog();
    }

    private void setCustomDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_bohui_layout, null);
        final EditText edtData = (EditText) mView.findViewById(R.id.edt_change_content_data);
        Button btnCancle = (Button) mView.findViewById(R.id.btn_change_content_cancle);
        Button btnEnter = (Button) mView.findViewById(R.id.btn_change_content_enter);
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bohui(edtData.getText().toString());
                dismiss();
            }
        });
        super.setContentView(mView);
    }

    @Override
    public void setContentView(int layoutResID) {
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    }

    @Override
    public void setContentView(View view) {
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 2:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    public void bohui(String content) {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);
        HttpBMethods.bohui(listener,
                shareCUserInfo.getId(),
                id, content);
    }
}
